/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.man.cs.afzal.tcm.information_retrieval;

import ac.man.cs.afzal.tcm.nlp.File_Manager_Impl;
import ac.man.cs.afzal.tcm.nlp.String_Manipulator_Impl;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;

public class Parse_PMC_XML
{

    File_Manager_Impl fileManager = new File_Manager_Impl();
    private String DOCUMENTS_CORPUS = "DOCUMENTS_CORPUS";

    private final String EXPRESSIONS_TO_BE_REMOVED[] =
    {
        "<sec>", "</sec>", "<st>", "</st>", "<it>", "</it>", "<abbrgrp>",
        "</abbrgrp>", "&#8226;", "&#160;",
        "<abbr bid=\"(.){0,3}\">(.){0,3}</abbr>",
        "<figr fid=\"(.){0,3}\">(.){0,7}</figr>",
        "<tblr tid=\"(.){0,3}\">(.){0,7}</tblr>",
        "<supplr sid=\"(.){0,3}\">(.){0,7}</supplr>",
        "<xref ref-type=\"(.){0,3}\">(.){0,7}</xref>",
        "<sec sec-type=\"methods\">"
    };
    private final String TAGS_TO_REPLACE_WITH_NEWLINE[] =
    {
        "<p>", "</p>"
    };
    private final String TAGS_TO_REPLACE_WITH_SPACE[] =
    {
        "<underline>", "</underline>", "<bold>", "</bold>", "<italic>", "</italic>", "<title>", "</title>", "</p>", "</sec>", "<sup>", "</sup>", "<sub>", "</sub>",
        "   "
    };
    String_Manipulator_Impl strMan = new String_Manipulator_Impl();

    public Parse_PMC_XML()
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
        }
        catch (Exception ex)
        {
            System.out.println("Exception while loading propoerties : " + ex.getCause());
        }
        DOCUMENTS_CORPUS = props.getProperty(DOCUMENTS_CORPUS);
    }

    /**
     * Converts the XML files of PubMedCentral XML format to text
     */
    public void convert_pmc_xml_to_text()
    {

        File dir = new File(DOCUMENTS_CORPUS);
        String[] str = dir.list();

        File xmlFile;

        for (String inputFileName : str)
        {
            System.out.println("Parsing " + inputFileName);
            xmlFile = new File(DOCUMENTS_CORPUS + inputFileName);
            String outputFileName = DOCUMENTS_CORPUS + xmlFile.getName();
            outputFileName = outputFileName.replace("xml", "txt");
            StringBuffer contents = new StringBuffer();
            StringBuffer parsed_contents = new StringBuffer();
            contents = fileManager.fileReader(xmlFile);

            String title = strMan.string_extractor(contents.toString(), "<article-title>", "</article-title>");
            title = remove_references(title);
            parsed_contents.append(title);
            parsed_contents.append(System.getProperty("line.separator"));

            String abstract_ = strMan.string_extractor(contents.toString(), "<abstract>", "</abstract>");
            abstract_ = remove_references(abstract_);
            abstract_ = extract_heading(abstract_);
            parsed_contents.append(abstract_);

            String trimmed_text = strMan.string_extractor(contents.toString(), "<body>", "</body>");

            trimmed_text = remove_references(trimmed_text);
            trimmed_text = extract_heading(trimmed_text);

            parsed_contents.append(trimmed_text);
            fileManager.fileWriter(parsed_contents, outputFileName);
        }
    }
    
    /*
     * It removes the expressions from the text to clean it. The removed expressions include
     * the equations, tables etc.
     */
    private String remove_references(String str)
    {
        for (String patternStr : EXPRESSIONS_TO_BE_REMOVED)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }
        for (String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }
        return str;
    }

    private String extract_heading(String str)
    {
        for (String patternStr : TAGS_TO_REPLACE_WITH_NEWLINE)
        {
            str = strMan.pattern_replacer(str, patternStr, "\n");
        }
        for (String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }

        str = strMan.string_replacer_start_zero(str, "<inline-formula>", "</inline-formula>", "");
        str = strMan.string_replacer_start_zero(str, "<m:math", "</m:math>", "");
        str = strMan.string_replacer_start_zero(str, "<graphic file", "/>", "");
        str = strMan.string_replacer_start_zero(str, "<fig id=", "</fig>", "");
        str = strMan.string_replacer_start_zero(str, "<tbl id=", "</tbl>", "");
        str = strMan.string_replacer_start_zero(str, "<suppl id=", "</suppl>", "");
        str = strMan.string_replacer_start_zero(str, "<xref ref-type=", "</xref>", "");
        str = strMan.string_replacer_start_zero(str, "<disp-formula", "</disp-formula>", "");
        str = strMan.string_replacer_start_zero(str, "<supplementary-material", "</supplementary-material>", "");
        str = strMan.string_replacer_start_zero(str, "<table-wrap", "</table-wrap>", "");
        str = strMan.string_replacer_start_zero(str, "<ext-link ext-link-type=", ">", "");
        str = strMan.string_replacer_start_zero(str, "<fig", "fig>", "");
        return str;
    }
}
